package com.example.myQuartz.service.impl;

import com.example.myQuartz.dao.ScheduleJobDao;
import com.example.myQuartz.dao.ScheduleJobLogDao;
import com.example.myQuartz.entity.ResultData;
import com.example.myQuartz.entity.ScheduleJobEntity;
import com.example.myQuartz.entity.ScheduleJobLogEntity;
import com.example.myQuartz.service.ScheduleJobService;
import com.example.myQuartz.task.ScheduleUtils;
import com.gitee.fastmybatis.core.query.Query;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("scheduleJobService")
public class ScheduleJobServiceImpl implements ScheduleJobService {
	@Autowired
    private Scheduler scheduler;

	@Autowired
	private ScheduleJobDao schedulerJobDao;

	@Autowired
	ScheduleJobLogDao jobLogDao;
	
	/**
	 * 项目启动时，初始化定时器
	 */
	@PostConstruct
	public void init(){
		List<ScheduleJobEntity> scheduleJobList = schedulerJobDao.queryList(new HashMap<>());
		for(ScheduleJobEntity scheduleJob : scheduleJobList){
			CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(scheduler, scheduleJob.getJobId());
            //如果不存在，则创建
            if(cronTrigger == null) {
                ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
            }else {
                ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
            }
		}
	}
	
	@Override
	public ScheduleJobEntity queryObject(Long jobId) {
		return schedulerJobDao.queryObject(jobId);
	}

	@Override
	public List<ScheduleJobEntity> queryList(Map<String, Object> map) {
		return schedulerJobDao.queryList(map);
	}

	@Override
	public int queryTotal(Map<String, Object> map) {
		return schedulerJobDao.queryTotal(map);
	}


	@Override
	@Transactional
	public void save(ScheduleJobEntity scheduleJob) {
		schedulerJobDao.save(scheduleJob);
        ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
    }
	
	@Override
	@Transactional
	public void update(ScheduleJobEntity scheduleJob) {
        ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
        schedulerJobDao.update(scheduleJob);
    }

	@Override
	@Transactional
    public void deleteBatch(Long[] jobIds) {
    	for(Long jobId : jobIds){
    		ScheduleUtils.deleteScheduleJob(scheduler, jobId);
    	}
    	//删除数据
    	schedulerJobDao.deleteBatch(jobIds);
	}

	@Override
    public int updateBatch(Long[] jobIds, int status){
    	Map<String, Object> map = new HashMap<>();
    	map.put("list", jobIds);
    	map.put("status", status);
    	return schedulerJobDao.updateBatchStatus(map);
    }
    
	@Override
	@Transactional
    public void run(Long[] jobIds) {
    	for(Long jobId : jobIds){
    		ScheduleUtils.run(scheduler, queryObject(jobId));
    	}
    }

	@Override
	@Transactional
    public void pause(Long[] jobIds) {
        for(Long jobId : jobIds){
    		ScheduleUtils.pauseJob(scheduler, jobId);
    	}
    	updateBatch(jobIds, 1);
    }

	@Override
	@Transactional
    public void resume(Long[] jobIds) {
    	for(Long jobId : jobIds){
    		ScheduleUtils.resumeJob(scheduler, jobId);
    	}
    	updateBatch(jobIds, 0);
    }

	@Override
	public ResultData listJob(int pageIndex, int pageSize) {
		ResultData resultData = new ResultData();
		//查询列表数据
		Query query = new Query();
		query.page(1,10);

		List<ScheduleJobEntity> jobList = schedulerJobDao.list(query);

		long total = schedulerJobDao.getCount(query);

		Map<String,Object> map = new HashMap<>();
		map.put("total",total);
		map.put("list",jobList);

		resultData.setData(map);
		return resultData;
	}

	@Override
	public ResultData queryLogObject(Long jobId) {
		ResultData resultData = new ResultData();
		resultData.setData(jobLogDao.getById(jobId));
		return resultData;
	}

	@Override
	public void saveLog(ScheduleJobLogEntity log) {
		jobLogDao.save(log);
	}

	@Override
	public ResultData listLog(int pageIndex, int pageSize) {
		ResultData resultData = new ResultData();

		//查询列表数据
		Query query = new Query();
		query.page(pageIndex,pageSize);

		List<ScheduleJobLogEntity> jobList = jobLogDao.list(query);

		long total = jobLogDao.getCount(query);

		Map<String,Object> map = new HashMap<>();
		map.put("total",total);
		map.put("list",jobList);

		resultData.setData(map);

		return resultData;
	}
}
